{% set profile = cookiecutter.template_profile %}
{% set agent_worktrees = cookiecutter.enable_agent_worktrees == "yes" or (cookiecutter.enable_agent_worktrees == "profile_default" and profile in ["agent_assisted", "github_sdlc", "software_factory"]) %}
{% set github_work_items = cookiecutter.enable_github_work_items == "yes" or (cookiecutter.enable_github_work_items == "profile_default" and profile in ["github_sdlc", "software_factory"]) %}
{% set role_prompts = cookiecutter.enable_role_prompts == "yes" or (cookiecutter.enable_role_prompts == "profile_default" and profile == "software_factory") %}
{% set protected_hooks = cookiecutter.enable_protected_main_hooks == "yes" or (cookiecutter.enable_protected_main_hooks == "profile_default" and profile in ["agent_assisted", "github_sdlc", "software_factory"]) %}
{% set drift_checks = cookiecutter.enable_quality_drift_checks == "yes" or (cookiecutter.enable_quality_drift_checks == "profile_default" and profile == "software_factory") %}
{% set dependency_audit = profile == "software_factory" %}
{% set initiation_bootstrap = profile == "software_factory" %}
{% set github_bootstrap = initiation_bootstrap and cookiecutter.work_item_backend in ["github_issues", "hybrid"] %}
{% set quality_tooling = profile != "minimal_python" or drift_checks %}
{% set has_quality_control = github_work_items or role_prompts or drift_checks or agent_worktrees %}
{% set has_presets = quality_tooling or has_quality_control %}
.PHONY: help{% if agent_worktrees %} setup-agent-policy agent-ensure agent-new agent-sync-main agent-worktrees agent-guard{% endif %}{% if github_work_items %} work-item-labels work-items-ready work-item-next work-item-claim work-item-status{% endif %}{% if github_bootstrap %} bootstrap-initiation-issues{% endif %}{% if initiation_bootstrap %} show-bootstrap-sequence{% endif %}{% if role_prompts %} agent-prompt{% endif %} check{% if quality_tooling %} lint format format-check typecheck{% endif %} test{% if has_quality_control %} quality-control{% endif %}{% if dependency_audit %} dependency-audit{% endif %}{% if github_work_items %} sdlc-check{% endif %}{% if has_presets %} ci-local-pr ci-local-governance ci-local-performance ci-local-release{% endif %}

PYTHON ?= python3
{% if agent_worktrees or github_work_items %}
AGENT ?= codex
{% endif %}
{% if agent_worktrees %}
TASK ?= task
{% endif %}
{% if github_work_items %}
ROLE ?= coding
ISSUE ?=
STATUS ?= in-progress
{% endif %}

help:
	@printf '%s\n' \
		'Common targets:' \
{% if agent_worktrees %}
		'  make setup-agent-policy' \
		'  make agent-ensure AGENT=claude TASK=my-task' \
{% endif %}
{% if github_work_items %}
		'  make work-item-next ROLE=coding' \
{% endif %}
{% if initiation_bootstrap %}
		'  make show-bootstrap-sequence' \
{% endif %}
{% if github_bootstrap %}
		'  make bootstrap-initiation-issues' \
{% endif %}
{% if role_prompts %}
		'  make agent-prompt ROLE=coding ISSUE=123' \
{% endif %}
{% if dependency_audit %}
		'  make dependency-audit' \
{% endif %}
		'  make check' \
{% if has_presets %}
		'  make ci-local-pr'
{% else %}
		'  make test'
{% endif %}

{% if agent_worktrees %}
setup-agent-policy:
	git config --local project.agentMainClone "$$(pwd)"
	git config --local project.agentWorktreeRoot "$$(dirname "$$(pwd)")/$$(basename "$$(pwd)")-worktrees"
{% if protected_hooks %}
	git config --local core.hooksPath .githooks
{% endif %}
	@echo "agent policy configured"

agent-ensure:
	$(PYTHON) scripts/agent_worktree.py ensure --agent $(AGENT) $(TASK)

agent-new:
	$(PYTHON) scripts/agent_worktree.py new --agent $(AGENT) $(TASK)

agent-sync-main:
	$(PYTHON) scripts/agent_worktree.py sync-main

agent-worktrees:
	$(PYTHON) scripts/agent_worktree.py list

agent-guard:
	$(PYTHON) scripts/agent_worktree.py guard

{% endif %}
{% if github_work_items %}
work-item-labels:
	$(PYTHON) scripts/sdlc/work_items.py labels

work-items-ready:
	$(PYTHON) scripts/sdlc/work_items.py list-ready

work-item-next:
	$(PYTHON) scripts/sdlc/work_items.py next --role $(ROLE)

work-item-claim:
	$(PYTHON) scripts/sdlc/work_items.py claim --issue $(ISSUE) --agent $(AGENT)

work-item-status:
	$(PYTHON) scripts/sdlc/work_items.py status --issue $(ISSUE) --status $(STATUS)

{% if github_bootstrap %}
bootstrap-initiation-issues:
	$(PYTHON) scripts/sdlc/work_items.py bootstrap-initiation-issues

{% endif %}
{% endif %}
{% if initiation_bootstrap %}
show-bootstrap-sequence:
{% if cookiecutter.work_item_backend == "github_issues" %}
	@printf '%s\n' \
		'Project initiation bootstrap:' \
		'  make work-item-labels' \
		'  make bootstrap-initiation-issues' \
		'  make work-item-next ROLE=pm' \
		'  make agent-prompt ROLE=pm ISSUE=<actual-github-issue-number>'
{% elif cookiecutter.work_item_backend == "hybrid" %}
	@printf '%s\n' \
		'Project initiation bootstrap:' \
		'  make work-item-labels' \
		'  make bootstrap-initiation-issues' \
		'  make work-item-next ROLE=pm' \
		'  make agent-prompt ROLE=pm ISSUE=<actual-github-issue-number>' \
		'  # File mirrors are under work_items/ready/WI-001... and WI-002...'
{% else %}
	@printf '%s\n' \
		'Project initiation bootstrap:' \
		'  make work-item-next ROLE=pm' \
		'  # Read work_items/ready/WI-001-mission-purpose-goals.md' \
		'  # Then update docs/MISSION_PURPOSE_AND_GOALS.md'
{% endif %}

{% endif %}
{% if role_prompts %}
agent-prompt:
	$(PYTHON) scripts/sdlc/agent_prompt.py --issue $(ISSUE) --role $(ROLE)

{% endif %}
check:{% if quality_tooling %} lint format-check typecheck{% endif %} test{% if has_quality_control %} quality-control{% endif %}{% if dependency_audit %} dependency-audit{% endif %}

{% if quality_tooling %}
lint:
	uv run ruff check .

format:
	uv run ruff format .

format-check:
	uv run ruff format --check .

typecheck:
	uv run mypy src/

{% endif %}
test:
	uv run pytest tests/

{% if has_quality_control %}
quality-control:
{% if drift_checks %}
	uv run python scripts/ci/check_code_drift.py
	uv run python scripts/ci/check_dead_code.py || true
	uv run python scripts/ci/check_simplification_drift.py || true
	uv run python scripts/ci/check_test_value.py || true
	uv run python scripts/ci/check_docstring_baseline.py
{% endif %}
{% if github_work_items %}
	python3 scripts/ci/check_work_item_links.py
{% endif %}
{% if role_prompts %}
	python3 scripts/ci/check_agent_prompt_alignment.py
{% endif %}
	python3 scripts/ci/check_version_bump.py
	python3 scripts/ci/check_markdown_links.py
{% if agent_worktrees %}
	python3 scripts/ci/check_agent_instruction_drift.py
{% endif %}

{% endif %}
{% if dependency_audit %}
dependency-audit:
	@if [ -f uv.lock ]; then \
		uv lock --check && \
		echo "Lock file is consistent with pyproject.toml"; \
	else \
		echo "No uv.lock present yet (first clone of a fresh scaffold)."; \
		echo "Run 'uv sync' locally, then commit uv.lock for reproducible installs."; \
	fi

{% endif %}
{% if github_work_items %}
sdlc-check:
	python3 scripts/ci/check_work_item_links.py

{% endif %}
{% if has_presets %}
ci-local-pr: check

ci-local-governance:
{% if has_quality_control %}
	$(MAKE) quality-control
{% else %}
	python3 scripts/ci/check_version_bump.py
	python3 scripts/ci/check_markdown_links.py
{% endif %}

ci-local-performance: check

ci-local-release: check
{% endif %}
